home *** CD-ROM | disk | FTP | other *** search
/ PC Media 7 / PC MEDIA CD07.iso / share / prog / cm / cmresfil.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-06  |  9.8 KB  |  222 lines

  1. // CmResFil.h
  2. // -----------------------------------------------------------------
  3. // Compendium - C++ Container Class Library
  4. // Copyright (C) 1992-1994, Glenn M. Poorman, All rights reserved
  5. // -----------------------------------------------------------------
  6. // Reserve binary file interface definition.
  7. // -----------------------------------------------------------------
  8.  
  9. #ifndef _CMRESFIL_H
  10. #define _CMRESFIL_H
  11.  
  12. #include <stdio.h>
  13. #include <cm/include/cmdefs.h>
  14.  
  15. class CmReserveFile {                         // File class definition.
  16. public:
  17.   CmReserveFile();                            // Default constructor.
  18.   CmReserveFile(const char*);                 // File constructor.
  19.   CmReserveFile(const CmReserveFile&);        // Copy constructor.
  20.  ~CmReserveFile();                            // File destructor.
  21.  
  22.   CmReserveFile& operator=(const CmReserveFile&);  // Assignment.
  23.  
  24.   Bool open (const char*);                    // Open named file.
  25.   Bool close();                               // Close the file.
  26.  
  27.   operator int() const;                       // Check if file is open.
  28.   Bool isValid() const;                       // Check if file is open.
  29.  
  30.   const char* name() const;                   // Return file name.
  31.  
  32.   Bool remove();                              // Close and remove file.
  33.   Bool rename(const char*);                   // Rename the file.
  34.  
  35.   size_t read (void*, size_t, size_t);        // Read buffers of size.
  36.   Bool   read (void*, size_t);                // Read one buffer of size.
  37.   size_t write(void*, size_t, size_t);        // Write buffers of size.
  38.   Bool   write(void*, size_t);                // Write one buffer of size.
  39.  
  40.   Bool read(char&);                           // Read one char.
  41.   Bool read(short&);                          // Read one short.
  42.   Bool read(int&);                            // Read one integer.
  43.   Bool read(long&);                           // Read one long.
  44.   Bool read(unsigned char&);                  // Read one unsigned char.
  45.   Bool read(unsigned short&);                 // Read one unsigned short.
  46.   Bool read(unsigned int&);                   // Read one unsigned int.
  47.   Bool read(unsigned long&);                  // Read one unsigned long.
  48.   Bool read(float&);                          // Read one float.
  49.   Bool read(double&);                         // Read one double.
  50.   Bool read(char*,   unsigned);               // Read a char array.
  51.   Bool read(short*,  unsigned);               // Read a short array.
  52.   Bool read(int*,    unsigned);               // Read an int array.
  53.   Bool read(long*,   unsigned);               // Read a long array.
  54.   Bool read(float*,  unsigned);               // Read a float array.
  55.   Bool read(double*, unsigned);               // Read a double array.
  56.  
  57.   Bool write(char);                           // Write one char.
  58.   Bool write(short);                          // Write one short.
  59.   Bool write(int);                            // Write one integer.
  60.   Bool write(long);                           // Write one long.
  61.   Bool write(unsigned char);                  // Write one unsigned char.
  62.   Bool write(unsigned short);                 // Write one unsigned short.
  63.   Bool write(unsigned int);                   // Write one unsigned int.
  64.   Bool write(unsigned long);                  // Write one unsigned long.
  65.   Bool write(float);                          // Write one float.
  66.   Bool write(double);                         // Write one double.
  67.   Bool write(char*,   unsigned);              // Write a char array.
  68.   Bool write(short*,  unsigned);              // Write a short array.
  69.   Bool write(int*,    unsigned);              // Write an int array.
  70.   Bool write(long*,   unsigned);              // Write a long array.
  71.   Bool write(float*,  unsigned);              // Write a float array.
  72.   Bool write(double*, unsigned);              // Write a double array.
  73.  
  74.   Bool  write(const char*);                   // Write strlen and string.
  75.   char* read ();                              // Read string written by above.
  76.  
  77.   int  seek  (long, int);                     // Set file position for stream.
  78.   long tell  ();                              // Return current file position.
  79.   void rewind();                              // Set position to beginning.
  80.   int  getPos(fpos_t*);                       // Get current position in ptr.
  81.   int  setPos(const fpos_t*);                 // Set position to ptr.
  82.  
  83.   void clearError();                          // Clear all error indicators.
  84.   Bool eof       ();                          // Check for end of file.
  85.   Bool error     ();                          // Check for error.
  86.   void printError(const char*);               // Print error message.
  87.  
  88.   static Bool  exists  (const char*);         // See if named file exists.
  89.   static Bool  remove  (const char*);         // Remove named file from disk.
  90.   static char* tempName(char*);               // Create non-existing file name.
  91.  
  92.   CmReserveFile& operator>>(char&);           // Read one char.
  93.   CmReserveFile& operator>>(short&);          // Read one short.
  94.   CmReserveFile& operator>>(int&);            // Read one integer.
  95.   CmReserveFile& operator>>(long&);           // Read one long.
  96.   CmReserveFile& operator>>(unsigned char&);  // Read one unsigned char.
  97.   CmReserveFile& operator>>(unsigned short&); // Read one unsigned short.
  98.   CmReserveFile& operator>>(unsigned int&);   // Read one unsigned int.
  99.   CmReserveFile& operator>>(unsigned long&);  // Read one unsigned long.
  100.   CmReserveFile& operator>>(float&);          // Read one float.
  101.   CmReserveFile& operator>>(double&);         // Read one double.
  102.   CmReserveFile& operator>>(char*&);          // Read strlen and string.
  103.  
  104.   CmReserveFile& operator<<(char);            // Write one char.
  105.   CmReserveFile& operator<<(short);           // Write one short.
  106.   CmReserveFile& operator<<(int);             // Write one integer.
  107.   CmReserveFile& operator<<(long);            // Write one long.
  108.   CmReserveFile& operator<<(unsigned char);   // Write one unsigned char.
  109.   CmReserveFile& operator<<(unsigned short);  // Write one unsigned short.
  110.   CmReserveFile& operator<<(unsigned int);    // Write one unsigned int.
  111.   CmReserveFile& operator<<(unsigned long);   // Write one unsigned long.
  112.   CmReserveFile& operator<<(float);           // Write one float.
  113.   CmReserveFile& operator<<(double);          // Write one double.
  114.   CmReserveFile& operator<<(const char*);     // Write strlen and string.
  115.  
  116. protected:
  117.   char *_name;                                // File name.
  118.   FILE *_file;                                // File stream pointer.
  119. };
  120.  
  121. // "isValid" checks if the file was successfully opened.
  122. inline Bool CmReserveFile::isValid() const
  123. { return (_file) ? TRUE : FALSE; }
  124.  
  125. // "int" operator checks if the file is currently open.
  126. inline CmReserveFile::operator int() const
  127. { return isValid(); }
  128.  
  129. // "name" returns the file name.
  130. inline const char* CmReserveFile::name() const
  131. { return _name; }
  132.  
  133. // ">>" operator reads one character.
  134. inline CmReserveFile& CmReserveFile::operator>>(char& c)
  135. { read(c); return *this; }
  136.  
  137. // ">>" operator reads one short.
  138. inline CmReserveFile& CmReserveFile::operator>>(short& s)
  139. { read(s); return *this; }
  140.  
  141. // ">>" operator reads one integer.
  142. inline CmReserveFile& CmReserveFile::operator>>(int& i)
  143. { read(i); return *this; }
  144.  
  145. // ">>" operator reads one long.
  146. inline CmReserveFile& CmReserveFile::operator>>(long& l)
  147. { read(l); return *this; }
  148.  
  149. // ">>" operator reads one unsigned character.
  150. inline CmReserveFile& CmReserveFile::operator>>(unsigned char& uc)
  151. { read(uc); return *this; }
  152.  
  153. // ">>" operator reads one unsigned short.
  154. inline CmReserveFile& CmReserveFile::operator>>(unsigned short& us)
  155. { read(us); return *this; }
  156.  
  157. // ">>" operator reads one unsigned integer.
  158. inline CmReserveFile& CmReserveFile::operator>>(unsigned int& ui)
  159. { read(ui); return *this; }
  160.  
  161. // ">>" operator reads one unsigned long.
  162. inline CmReserveFile& CmReserveFile::operator>>(unsigned long& ul)
  163. { read(ul); return *this; }
  164.  
  165. // ">>" operator reads one float.
  166. inline CmReserveFile& CmReserveFile::operator>>(float& f)
  167. { read(f); return *this; }
  168.  
  169. // ">>" operator reads one long.
  170. inline CmReserveFile& CmReserveFile::operator>>(double& d)
  171. { read(d); return *this; }
  172.  
  173. // ">>" operator reads string length and string text.
  174. inline CmReserveFile& CmReserveFile::operator>>(char*& s)
  175. { s = read(); return *this; }
  176.  
  177. // "<<" operator writes one character.
  178. inline CmReserveFile& CmReserveFile::operator<<(char c)
  179. { write(c); return *this; }
  180.  
  181. // "<<" operator writes one short.
  182. inline CmReserveFile& CmReserveFile::operator<<(short s)
  183. { write(s); return *this; }
  184.  
  185. // "<<" operator writes one integer.
  186. inline CmReserveFile& CmReserveFile::operator<<(int i)
  187. { write(i); return *this; }
  188.  
  189. // "<<" operator writes one long.
  190. inline CmReserveFile& CmReserveFile::operator<<(long l)
  191. { write(l); return *this; }
  192.  
  193. // "<<" operator writes one unsigned character.
  194. inline CmReserveFile& CmReserveFile::operator<<(unsigned char uc)
  195. { write(uc); return *this; }
  196.  
  197. // "<<" operator writes one unsigned short.
  198. inline CmReserveFile& CmReserveFile::operator<<(unsigned short us)
  199. { write(us); return *this; }
  200.  
  201. // "<<" operator writes one unsigned integer.
  202. inline CmReserveFile& CmReserveFile::operator<<(unsigned int ui)
  203. { write(ui); return *this; }
  204.  
  205. // "<<" operator writes one unsigned long.
  206. inline CmReserveFile& CmReserveFile::operator<<(unsigned long ul)
  207. { write(ul); return *this; }
  208.  
  209. // "<<" operator writes one unsigned float.
  210. inline CmReserveFile& CmReserveFile::operator<<(float f)
  211. { write(f); return *this; }
  212.  
  213. // "<<" operator writes one unsigned double.
  214. inline CmReserveFile& CmReserveFile::operator<<(double d)
  215. { write(d); return *this; }
  216.  
  217. // "<<" outputs the string length and string text.
  218. inline CmReserveFile& CmReserveFile::operator<<(const char* s)
  219. { write(s); return *this; }
  220.  
  221. #endif
  222.